


$(document).ready(function() {
    /* var current_page = 1;
     $('#book').turn(
     );
     setInterval(nextSlide, 2000);
     function nextSlide() {
     $("#book").turn('next');
     
     current_page += 2;
     
     }
     $("#book").bind('turned',
     function(e, page, obj) {
     if ($("#book").turn('pages') <= current_page)
     {
     // $("#book").turn('page', 1);
     //current_page = 1;
     var x=$("#book :first-child").html();
     $("#book").remove(x);
     $("#book").append("#book");
     }
     }
     )
     */
    $(function() {
        $("#book").booklet({auto: true, delay: 2000, keyboard: true, arrows: false, arrowsHide: true, width: 1000, height: 270});
    });

    /*  $("#bb-nav-prev").click(function() {
     $("#book").turn("previous");
     });
     $("#bb-nav-next").click(function() {
     $("#book").turn("next");
     });
     */
    $(".catalog").hover(function() {

        $(this).stop().animate({marginLeft: '20px', 'z-index': '+=15'}, 300);

        //$(this).animate({left:"=30",z-index:"15"},300).animate({left:"=-30",z-index:"10"},300);
    }, function() {
        $(this).stop().animate({marginLeft: '0px', 'z-index': '+=15'}, 300);
    });
    $("#logo").hover(function() {

        $(this).stop().animate({marginLeft: '20px', 'z-index': '+=15'}, 600);

        //$(this).animate({left:"=30",z-index:"15"},300).animate({left:"=-30",z-index:"10"},300);
    }, function() {
        $(this).stop().animate({marginLeft: '0px', 'z-index': '+=15'}, 600);
    });
    btnlogin = 0;
    function angiohang() {
        $("#listgiohang").toggle(500);//.slideUp(500) ;//.css({"display": "none"});
        btngiohang = 0;
    }
    function  hiengiohang() {
        $("#listgiohang").toggle(500);//.css({"display": "block"});
        btngiohang = 1;
        anlogin();
    }
    btngiohang = 0;
    $("#btn_giohang").click(function() {
        if (btngiohang == 0)
        {
            if ($("#soluongsp").val() != "0")
                $.ajax({
                    type: "GET",
                    url: globalurl + 'index.php/carts/show',
                    data: {
                    },
                    success: function(data)
                    {

                        // $("#sosp").val();
                        $("#listgiohang").html(data);
                        hiengiohang();
                    }
                });

        }
        else
            angiohang();
    });
    function  anlogin() {
        $("#form-login").slideUp(500);
        btnlogin = 0;
    }
    function hienlogin() {

        $("#form-login").slideDown(500);
        btnlogin = 1;
        if (btngiohang == 1)
            angiohang();

    }
    $("#buttonlogin").click(function() {
        //alert("a");
        if (btnlogin === 0)
        {
            hienlogin();


        }
        else
            anlogin();
    });
    btntaikhoan = 0;
    $("#buttontaikhoan").click(function() {
        //alert("a");
        if (btntaikhoan == 0)
        {

            $("#form-taikhoan").slideDown(500);
            btntaikhoan = 1;
        }
        else {
            $("#form-taikhoan").slideUp(500);
            btntaikhoan = 0;
        }
    });

    $("#buttonlogout").click(function() {
        if (confirm('Bạn có chắc chắn muốn thoát ra không'))
        {
            window.location = globalurl + "index.php/taikhoan/thoat";
        }
    });
    $(".remove").click(function() {
        if (confirm('Bạn có muốn xóa sản phẩm này không?')) {
            alert($(this).html());
            $rowid = $(this).attr("id");
            alert($rowid);
            $.ajax({
                type: "GET",
                url: globalurl + 'index.php/carts/update',
                data: {
                    rowid: $rowid,
                    qty: 0
                },
                success: function(data)
                {
                    location.reload();
                }
            });
        }
    });

    $(".add_to_cart").click(function(e) {

        var a = $(this).parent().parent();
        var id = a.find(".matp").html();
        var name = a.find(".tentp").html();
        var price = a.find(".price").html();
        var qty = 1;

        $.ajax({
            type: "GET",
            url: globalurl + 'index.php/carts/add',
            data: {
                id: id,
                qty: qty,
                name: name,
                price: price
            },
            success: function(data)
            {
                $("#sosp").text((parseInt($("#sosp").text()) + 1));
                $("#listgiohang").html(data);
                $('#popup').bPopup({
                    easing: 'easeOutBack', //uses jQuery easing plugin
                    speed: 450,
                    modalClose: true
                }, function() {
                    $(".b-close").click();
                });
            }
        });
    });


    $("#soluongAdd").click(function() {
        var soluong = parseInt($(".txtsoluong").val());

        $(".txtsoluong").val(soluong + 1);
    });
    $("#soluongSubStract").click(function() {
        var soluong = parseInt($(".txtsoluong").val());
        if (soluong < 2)
            return;
        $(".txtsoluong").val(soluong - 1);
    });
    $("#btn_mua").click(function() {
        var qty=$("#soluong").val();
        var name=$("#tentp").text();
        var price=$("#giaban").text();
        var id=$("#matp").text();
     
       //alert("sl"+qty+"ten"+name+"gia"+price+"id"+id);
        $.ajax({
            type: "GET",
            url: globalurl + 'index.php/carts/add',
            data: {
                id: id,
                qty: qty,
                name:name,
                price:price
            },
            success: function(data)
            {
                alert(data);
                $("#danhsachsp").html(data);
            }, error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError);
            }
        });

    });
    function convertVietnamese(str) { 
    str= str.toLowerCase();
    str= str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a"); 
    str= str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e"); 
    str= str.replace(/ì|í|ị|ỉ|ĩ/g,"i"); 
    str= str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o"); 
    str= str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u"); 
    str= str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y"); 
    str= str.replace(/đ/g,"d"); 
    str= str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g,"-");
    str= str.replace(/-+-/g,"-");
    str= str.replace(/^\-+|\-+$/g,""); 

    return str; 
}
    $(".btn_search").click(function() {
        //alert("soluong");
        var str = convertVietnamese($("#txt_search").val());
      
        window.location=globalurl+"index.php/homepage/timkiem/"+str;
    });
});

///////
$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = {forceAbsolute: false,
        container: window, // selector of element to center in
        completeHandler: null
    };
    $.extend(opt, options);

    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}